package com.sogoodsoft.web.webservice.security;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;

import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.UnsupportedCallbackException;
import javax.servlet.http.HttpServlet;

import org.apache.axis.MessageContext;
import org.apache.axis.transport.http.HTTPConstants;
import org.apache.ws.security.WSPasswordCallback;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.sogoodsoft.entity.account.User;
import com.sogoodsoft.service.account.AccountManager;

public class InfoSysSecurity implements CallbackHandler {

	private AccountManager manage;
	protected Logger logger = LoggerFactory.getLogger(getClass());
	public InfoSysSecurity() {
		MessageContext msgContext = MessageContext.getCurrentContext();
		HttpServlet servlet = (HttpServlet) msgContext
				.getProperty(HTTPConstants.MC_HTTP_SERVLET);
		WebApplicationContext appContext = WebApplicationContextUtils
				.getRequiredWebApplicationContext(servlet.getServletContext());
		manage = (AccountManager) appContext.getBean("accountManager");
	}

	public void handle(Callback[] callbacks) throws IOException,
			UnsupportedCallbackException {
		// TODO Auto-generated method stub
		for (int i = 0; i < callbacks.length; i++) {
			if (callbacks[i] instanceof WSPasswordCallback) {
				WSPasswordCallback wspc = (WSPasswordCallback) callbacks[i];
				String loginName = wspc.getIdentifer();
				if(logger.isDebugEnabled()){
					logger.debug("loginname="+loginName);
				}
				
				try {
					File file = new File("target"+File.separator+"classes"+File.separator+"cert"+File.separator+"infosys.keystore");		
					System.out.println(file.getAbsolutePath());
					FileInputStream fin = new FileInputStream(file.getAbsolutePath());
					KeyStore kstore = KeyStore.getInstance("jks");
					kstore.load(fin, "admin123".toCharArray());
					Certificate cert =  kstore.getCertificate(loginName);
					PublicKey pkey = cert.getPublicKey();
					wspc.setKey(pkey.getEncoded());
					wspc.setPassword("admin123");

				} catch (KeyStoreException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (NoSuchAlgorithmException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (CertificateException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} 
				User user = manage.findUserByLoginName(loginName);
				if (user != null)
					wspc.setPassword(user.getPassword());
			} else {
				throw new UnsupportedCallbackException(callbacks[i],
						"Unrecognized Callback");
			}
		}
	}

}
